import { Body, Controller, Get, Post, Req } from '@nestjs/common';
import { AppUserService } from './user.service';
// import { EditDto } from './dto/edit.dto';
// import { EditInfoDto } from './dto/edit-info.dto';
// import { AddDto } from './dto/add.dto';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { ApiRes } from 'src/common/decorator/api-res.decorator';
import { AppAuthService } from '../auth/auth.service';
import { AppUser } from './user.entity';
import { EditAppUserInfoDto } from './dto/edit-info.dto';
import { AppUserReadStatisticsQueryDto } from './dto/query.dto';
import { LoginResDto } from '../auth/dto/login.dot';

@ApiTags('app用户模块')
@ApiBearerAuth()
@Controller('app/user')
export class AppUserController {
  constructor(
    private readonly userService: AppUserService,
    private readonly appAuthService: AppAuthService,
  ) {}

  // @Post('/list')
  // @ApiRes(User, true)
  // async findAll(@Body() params: AppUserListDto) {
  //   return {
  //     data: await this.userService.findAll(params),
  //   };
  // }

  @ApiOperation({
    summary: '查询用户信息',
  })
  @Get('/query')
  @ApiRes(AppUser)
  async query(@Req() request) {
    const {
      auth: { uid },
    } = request;
    return {
      data: await this.userService.getUserDetail(uid),
    };
  }

  @ApiOperation({
    summary: '查询用户阅读统计',
  })
  @Get('/query-read-statistics')
  @ApiRes(AppUserReadStatisticsQueryDto)
  async queryReadStatistics(@Req() request) {
    const {
      auth: { uid },
    } = request;
    return {
      data: await this.userService.getUserHistory(uid),
    };
  }
  // @ApiOperation({
  //   summary: '用户是否是vip,  0否1是',
  // })
  // @Get('/getUserVip')
  // @ApiRes(Number)
  // async getUserVip(@Query() params: AppUserQueryDto) {
  //   const user = await this.userService.getUserById(params.id);
  //   if (!user) {
  //     return {
  //       code: '100000',
  //       message: '用户不存在',
  //     };
  //   }
  //   if (new Date(user.vipExpiredAt).getTime() < new Date().getTime()) {
  //     return {
  //       data: 0,
  //     };
  //   }
  //   return {
  //     data: user.vipLevel,
  //   };
  // }

  // @Get('/userInfo')
  // async userInfo(@Req() request) {
  //   const { uid } = request.auth;
  //   return {
  //     data: await this.userService.getUserDetail(uid),
  //   };
  // }

  @Post('/edit')
  @ApiOperation({
    summary: '编辑用户信息',
  })
  async edit(@Body() user: EditAppUserInfoDto, @Req() request) {
    const { uid } = request.auth;
    await this.userService.update(uid, user);
    return {
      data: null,
    };
  }

  // @Post('/editInfo')
  // async editInfo(@Req() request, @Body() user: EditInfoDto) {
  //   const { uid } = request.auth;
  //   return {
  //     data: await this.userService.updateInfo(uid, user),
  //   };
  // }

  // @Post('/add')
  // async add(@Body() user: AddAppUserDto) {
  //   user.initial = strToPinyinCode(user.name);
  //   return {
  //     data: await this.userService.createUser(user),
  //   };
  // }

  @ApiOperation({
    summary: '刷新token',
  })
  @ApiRes(LoginResDto)
  @Get('/refresh-token')
  async refresh(@Req() request) {
    const { uid, vip } = request.auth;
    return {
      data: {
        token: this.appAuthService.getToken({
          userId: uid,
          vipLevel: vip,
        } as AppUser),
      },
    };
  }
}
